package com.usian.mapper;

import com.usian.comment.Comment;
import com.usian.feedback.Feedback;
import com.usian.square.Square;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import com.usian.user.User;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

@Mapper
public interface SquareMapper {

    @Select("<script>" +
            "SELECT * FROM square" +
            "<where>" +
            "    1=1" +
            "    <if test='title != null and title != \"\"'> " +
            "        AND title LIKE CONCAT('%', #{title}, '%') " +
            "    </if>" +
            "</where>" +
            "</script>")
    List<Square> findAll(Square square);

    @Select("select * from user where id=#{user_id}")
    User selectUserById(Integer userId);

    @Insert("insert into square(title,content,place,yield,type,comment,praise,userId,image)\n" +
            "        values(#{title},#{content},#{place},#{yield},#{type},#{comment},#{praise},#{userId},#{image})")
    int add(Square square);

    @Select("select * from square where userId=#{userId}")
    List<Square> findById(Integer userId);

    @Select("select * from user where id=#{id}")
    User selectUserByIdTwo(Integer id);

    @Select("select * from comment where sid=#{id} and status=#{status}")
    List<Comment> comments(Integer id, Integer status);

    @Select("select * from user where id=#{userId}")
    User selectUserByIdThree(Integer userId);

    @Insert("insert into comment(userId,context,sid,status) values(#{userId},#{context},#{sid},#{status})")
    int addComment(Comment comment);

    @Update("update square set praise = praise+1 where id=#{id}")
    int praise(Integer id);

    @Select("select sid,count(*) as count from comment group by sid")
    List<Map<String, Object>> comment();

    @Insert("insert into feedback(type,advice,img,userId) values(#{type},#{advice},#{img},#{userId})")
    int feedback(Feedback feedback);
}
